package com.matrix.merchant.converter;

import com.matrix.api.merchant.dto.MerchantApplicationDTO;
import com.matrix.api.merchant.dto.ShopTableDTO;
import com.matrix.api.merchant.dto.ShopUpdateDTO;
import com.matrix.api.merchant.vo.MerchantApplicationVO;
import com.matrix.api.merchant.vo.ShopBriefInfoVO;
import com.matrix.api.merchant.vo.ShopDetailVO;
import com.matrix.api.merchant.vo.ShopTableVO;
import com.matrix.merchant.entity.MerchantApplication;
import com.matrix.merchant.entity.Shop;
import com.matrix.merchant.entity.ShopTable;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author 有点甜
 * @since 2025/8/5
 */
@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface MerchantConverter {

    MerchantConverter INSTANCE = Mappers.getMapper(MerchantConverter.class);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "userId", ignore = true)
    @Mapping(target = "status", ignore = true)
    @Mapping(target = "rejectionReason", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "latitude", expression = "java(java.math.BigDecimal.valueOf(dto.getLatitude()))")
    @Mapping(target = "longitude", expression = "java(java.math.BigDecimal.valueOf(dto.getLongitude()))")
    MerchantApplication toMerchantApplication(MerchantApplicationDTO dto);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "shopId", ignore = true)
    @Mapping(target = "status", ignore = true)
    @Mapping(target = "qrCodeUrl", ignore = true)
    @Mapping(target = "currentOrderId", ignore = true)
    ShopTable toShopTable(ShopTableDTO dto);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "ownerId", ignore = true)
    @Mapping(target = "rating", ignore = true)
    @Mapping(target = "avgConsumption", ignore = true)
    @Mapping(target = "tasteRating", ignore = true)
    @Mapping(target = "packagingRating", ignore = true)
    @Mapping(target = "status", ignore = true)
    @Mapping(target = "monthlySales", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    void updateShopFromDTO(ShopUpdateDTO dto, @MappingTarget Shop shop);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "shopId", ignore = true)
    @Mapping(target = "qrCodeUrl", ignore = true)
    @Mapping(target = "status", ignore = true)
    @Mapping(target = "currentOrderId", ignore = true)
    void updateShopTableFromDTO(ShopTableDTO dto, @MappingTarget ShopTable table);

    List<MerchantApplicationVO> toMerchantApplicationVOList(List<MerchantApplication> applications);

    @Mapping(source = "id", target = "shopId")
    @Mapping(target = "environmentPhotos", ignore = true)
    ShopDetailVO toShopDetailVO(Shop shop);

    @Mapping(source = "id", target = "shopId")
    @Mapping(target = "ownerPhone", ignore = true)
    ShopBriefInfoVO toShopBriefInfoVO(Shop shop);

    List<ShopBriefInfoVO> toShopBriefInfoVOList(List<Shop> shops);

    ShopTableVO toShopTableVO(ShopTable table);
}
